/* 用户模块的vuex操作 */
import { getUserInfo } from '@/api/user'
import { Toast } from 'vant'
import {
  setStorageToken,
  getStorageToken,
  removeStorageToken
} from '@/utils/storage'

const state = {
  token: getStorageToken(),
  // 用户的个人信息
  userInfo: {}
}

const mutations = {
  setToken(state, payload) {
    state.token = payload

    setStorageToken(payload)
  },
  setUserInfo(state, payload) {
    state.userInfo = payload
  },
  removeToken(state) {
    state.token = {}

    removeStorageToken()
  }
}
const getters = {}
const actions = {
  // 获取用户信息
  async Actions_getUserInfo(context) {
    try {
      const res = await getUserInfo()
      context.commit('setUserInfo', res.data)
    } catch {
      Toast('获取用户信息失败')
    }
  }
}
export default {
  state,
  mutations,
  getters,
  actions,
  namespaced: true
}
